package com.study.mvc.controller;

import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Date;
import java.util.UUID;

/**
 * @author wei.zhao
 * @date 2021年11月29日 15:29
 */
@Controller //添加该注解，指定该类为一个请求处理器，不需要实现任何接口或者继承任何类。
public class TestController {

    /* 逻辑视图名 */
    @RequestMapping("showView")//在方法上或者类上添加该注解，指定请求的url由该方法处理。
    public String showView() {
        return "hello";
    }

    /* json数据 */
    @ResponseBody
    @RequestMapping("showData")
    public String showData() {
        return "showData";
    }

    /* ModelAndView */
    @GetMapping("/modelAndView")
    public ModelAndView modelAndView() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("hello");
        return modelAndView;
    }

    /* 转发 */
    @GetMapping("/dispatcher")
    public void dispatcher(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.getRequestDispatcher("showData").forward(request, response);
    }

    /* 转发-方式二 */
    @GetMapping("/dispatcher2")
    public String dispatcher2(HttpServletRequest request, HttpServletResponse response) {
        return "forward:showData";
    }

    /* 重定向 */
    @GetMapping("/redirect")
    public void redirect(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.sendRedirect("https://www.baidu.com");
    }

    /* 重定向-方式二 */
    @GetMapping("/redirect2")
    public String redirect2(HttpServletRequest request, HttpServletResponse response) throws Exception {
        new MappingJackson2HttpMessageConverter();
        return "redirect:https://www.baidu.com";
    }

    /* response输出 */
    @GetMapping("/responseOut")
    public void responseOut(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write("response输出");
    }

    /* 测试日期格式转换 */
    @ResponseBody
    @PostMapping("/testDateConvert")
    public Date testDateConvert(@RequestParam("date") Date date) {
        return date;
    }

    /* 测试文件上传 */
    @RequestMapping("/fileupload")
    public String fileupload(MultipartFile uploadFile) throws Exception {
        if (uploadFile != null) {
            // 原始图片名称
            String originalFilename = uploadFile.getOriginalFilename();
            // 如果没有图片名称，则上传不成功
            if (originalFilename != null && originalFilename.length() > 0) {
                // 存放图片的物理路径
                String picPath = "E:\\";
                // 获取上传文件的扩展名
                String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
                // 新文件的名称
                String newFileName = UUID.randomUUID() + extName;
                // 新的文件
                File newFile = new File(picPath + newFileName);
                // 把上传的文件保存成一个新的文件
                uploadFile.transferTo(newFile);
            }
        }
        return "文件上传成功";
    }

}
